package org.dfzt.modules.online.config.service.a;

import org.apache.commons.lang.StringUtils;
import org.dfzt.modules.online.config.service.DbTableHandleI;
import org.dfzt.modules.online.config.util.a;


public class OracleHandler implements DbTableHandleI {
    public String getAddColumnSql(a columnMeta) {
        return " ADD  " + a(columnMeta) + "";
    }


    public String getReNameFieldName(a columnMeta) {
        return "RENAME COLUMN  " + columnMeta.getOldColumnName() + " TO " + columnMeta.getColumnName() + "";
    }


    public String getUpdateColumnSql(a cgformcolumnMeta, a datacolumnMeta) {
        return " MODIFY   " + a(cgformcolumnMeta, datacolumnMeta) + "";
    }


    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String str = "";
        if (dataType.equalsIgnoreCase("varchar2"))
            str = "string";
        if (dataType.equalsIgnoreCase("nvarchar2")) {
            str = "string";
        } else if (dataType.equalsIgnoreCase("double")) {
            str = "double";
        } else if (dataType.equalsIgnoreCase("number") && digits == 0) {
            str = "int";
        } else if (dataType.equalsIgnoreCase("number") && digits != 0) {
            str = "double";
        } else if (dataType.equalsIgnoreCase("int")) {
            str = "int";
        } else if (dataType.equalsIgnoreCase("Date")) {
            str = "date";
        } else if (dataType.equalsIgnoreCase("Datetime")) {
            str = "date";
        } else if (dataType.equalsIgnoreCase("blob")) {
            str = "blob";
        } else if (dataType.equalsIgnoreCase("clob")) {
            str = "text";
        }

        return str;
    }


    public String dropTableSQL(String tableName) {
        return " DROP TABLE  " + tableName.toLowerCase() + " ";
    }


    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName.toUpperCase() + "";
    }


    private String a(a parama) {
        String n = "";
        if (parama.getColunmType().equalsIgnoreCase("string")) {
          n = parama.getColumnName() + " varchar2(" + parama.getColumnSize() + ")";
        } else if (parama.getColunmType().equalsIgnoreCase("date")) {
          n = parama.getColumnName() + " date";
        } else if (parama.getColunmType().equalsIgnoreCase("int")) {
          n = parama.getColumnName() + " NUMBER(" + parama.getColumnSize() + ")";
        } else if (parama.getColunmType().equalsIgnoreCase("double")) {
          n = parama.getColumnName() + " NUMBER(" + parama.getColumnSize() + "," + parama.getDecimalDigits() + ")";
        } else if (parama.getColunmType().equalsIgnoreCase("bigdecimal")) {
          n = parama.getColumnName() + " NUMBER(" + parama.getColumnSize() + "," + parama.getDecimalDigits() + ")";
        } else if (parama.getColunmType().equalsIgnoreCase("text")) {
          n = parama.getColumnName() + " CLOB ";
        } else if (parama.getColunmType().equalsIgnoreCase("blob")) {
          n = parama.getColumnName() + " BLOB ";
        }
      n = n + (StringUtils.isNotEmpty(parama.getFieldDefault()) ? (" DEFAULT " + parama.getFieldDefault()) : " ");
        return n + ("Y".equals(parama.getIsNullable()) ? " NULL" : " NOT NULL");
    }

    private String a(a parama1, a parama2) {
        String n = "";
        String str = "";

        if (!parama2.getIsNullable().equals(parama1.getIsNullable())) {
            str = parama1.getIsNullable().equals("Y") ? "NULL" : "NOT NULL";
        }

        if (parama1.getColunmType().equalsIgnoreCase("string")) {
            n = parama1.getColumnName() + " varchar2(" + parama1.getColumnSize() + ")" + str;
        } else if (parama1.getColunmType().equalsIgnoreCase("date")) {
            n = parama1.getColumnName() + " date " + str;
        } else if (parama1.getColunmType().equalsIgnoreCase("int")) {
            n = parama1.getColumnName() + " NUMBER(" + parama1.getColumnSize() + ") " + str;
        } else if (parama1.getColunmType().equalsIgnoreCase("double")) {
            n = parama1.getColumnName() + " NUMBER(" + parama1.getColumnSize() + "," + parama1.getDecimalDigits() + ") " + str;
        } else if (parama1.getColunmType().equalsIgnoreCase("bigdecimal")) {
            n = parama1.getColumnName() + " NUMBER(" + parama1.getColumnSize() + "," + parama1.getDecimalDigits() + ") " + str;
        } else if (parama1.getColunmType().equalsIgnoreCase("blob")) {
            n = parama1.getColumnName() + " BLOB " + str;
        } else if (parama1.getColunmType().equalsIgnoreCase("text")) {
            n = parama1.getColumnName() + " CLOB " + str;
        }

        n = n + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" DEFAULT " + parama1.getFieldDefault()) : " ");
        return n + str;
    }


    public String getCommentSql(a columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + "'";
    }


    public String getSpecialHandle(a cgformcolumnMeta, a datacolumnMeta) {
        return null;
    }


    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }


    public String countIndex(String indexName, String tableName) {
        return "select count(*) from user_ind_columns where index_name=upper('" + indexName + "')";
    }
}
